<template>
  <div class="wms-layout">
    <el-container class="wms-container">
      <!-- 顶部导航栏 -->
      <el-header class="wms-header">
        <div class="header-content">
          <!-- 左侧品牌区域 -->
          <div class="brand-section">
            <div class="logo-container">
              <img alt="System Logo" class="logo" src="@/assets/logo.svg" />
              <div class="brand-text">
                <h1 class="system-title">智能仓储管理系统</h1>
                <span class="system-subtitle">Intelligent WMS Platform</span>
              </div>
            </div>
          </div>

          <!-- 中间状态指示器 -->
          <div class="status-section">
            <div class="status-indicators">
              <div class="status-item">
                <el-icon class="status-icon online"><CircleCheckFilled /></el-icon>
                <span class="status-text">系统运行正常</span>
              </div>
              
              <!-- SignalR状态指示器 -->
              <div class="status-item">
                <el-icon 
                  class="status-icon" 
                  :class="signalRStatus"
                >
                  <Connection v-if="signalRStatus === 'online'" />
                  <Loading v-else-if="signalRStatus === 'connecting'" />
                  <Close v-else />
                </el-icon>
                <span class="status-text">{{ signalRStatusText }}</span>
              </div>
              
              <div class="system-time">
                {{ currentTime }}
              </div>
            </div>
          </div>

          <!-- 右侧用户区域 -->
          <div class="user-section">
            <div class="user-info-card">
              <div class="user-avatar">
                <el-avatar :size="36" :src="userAvatar" class="avatar">
                  <el-icon><User /></el-icon>
                </el-avatar>
              </div>
              <div class="user-details">
                <div class="user-name">{{ useinfo.UserInfo.UserName }}</div>
                <div class="user-role">{{ useinfo.UserInfo.RoleName }}</div>
              </div>
            </div>
            
            <div class="action-buttons">
              <el-tooltip content="系统管理" placement="bottom">
                <el-button 
                  class="action-btn" 
                  type="primary" 
                  :icon="Setting" 
                  @click="manager()" 
                  circle 
                />
              </el-tooltip>
              <el-tooltip content="退出登录" placement="bottom">
                <el-button 
                  class="action-btn logout-btn" 
                  type="danger" 
                  :icon="SwitchButton" 
                  @click="tui" 
                  circle 
                />
              </el-tooltip>
            </div>
          </div>
        </div>
      </el-header>

      <el-container class="main-container">
        <!-- 侧边菜单栏 -->
        <el-aside class="wms-sidebar" :width="sidebarWidth">
          <div class="sidebar-header">
            <div class="menu-title">
              <el-icon class="menu-icon"><Menu /></el-icon>
              <span v-show="!isCollapsed" class="title-text">功能导航</span>
            </div>
            <el-button 
              v-show="!isCollapsed"
              class="collapse-btn" 
              :icon="Fold" 
              @click="toggleSidebar"
              text
            />
          </div>

          <el-scrollbar class="menu-scrollbar">
            <el-menu
              :default-active="route.path"
              class="wms-menu"
              :collapse="isCollapsed"
              :collapse-transition="false"
              router
            >
              <el-sub-menu 
                v-for="menuItem in datas" 
                :key="menuItem.url"
                :index="menuItem.url"
                class="main-menu-item"
              >
                <template #title>
                  <el-icon class="menu-item-icon">
                    <component :is="getMenuIcon(menuItem.label)" />
                  </el-icon>
                  <span class="menu-item-text">{{ menuItem.label }}</span>
                </template>
                
                <el-menu-item 
                  v-for="subItem in menuItem.children" 
                  :key="subItem.url"
                  :index="subItem.url"
                  class="sub-menu-item"
                >
                  <el-icon class="sub-menu-icon">
                    <component :is="getSubMenuIcon(subItem.label)" />
                  </el-icon>
                  <span class="sub-menu-text">{{ subItem.label }}</span>
                </el-menu-item>
              </el-sub-menu>
            </el-menu>
          </el-scrollbar>

          <!-- 侧边栏底部 -->
          <div class="sidebar-footer" v-show="!isCollapsed">
            <div class="footer-info">
              <div class="version-info">v2.1.0</div>
              <div class="copyright">© 2024 WMS</div>
            </div>
          </div>
        </el-aside>

        <!-- 主内容区域 -->
        <el-main class="wms-main">
          <div class="content-wrapper">
            <div class="breadcrumb-section">
              <el-breadcrumb separator="/" class="page-breadcrumb">
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>{{ getCurrentBreadcrumb() }}</el-breadcrumb-item>
              </el-breadcrumb>
            </div>
            
            <div class="main-content">
              <RouterView />
            </div>
          </div>
        </el-main>
      </el-container>
    </el-container>

    <!-- 展开按钮（仅在收起状态显示） -->
    <div v-if="isCollapsed" class="expand-trigger" @click="toggleSidebar">
      <el-icon><Expand /></el-icon>
    </div>


  </div>
</template>


  


<script lang="ts" setup>
import { onMounted, ref, computed, onBeforeUnmount } from 'vue'
import {
  Menu,
  SwitchButton,
  Location,
  Setting,
  User,
  CircleCheckFilled,
  Fold,
  Expand,
  Box,
  Document,
  Monitor,
  UserFilled,
  Management,
  DataAnalysis,
  Goods,
  List,
  Plus,
  Search,
  EditPen,
  Delete,
  View,
  Operation,
  TrendCharts,
  Files,
  SetUp,
  Tools,
  Connection,
  Close,
  Loading
} from '@element-plus/icons-vue'
import { useRoute, useRouter } from 'vue-router'
import { useCounterStore } from '@/stores/counter'
import { readBasic } from '@/Helper/axiosSetting'
import { ElMessage } from 'element-plus'
import { signalRService } from '@/services/SignalRService'

const useinfo = useCounterStore()
const route = useRoute()
const router = useRouter()
const datas = ref<any>([])

// 侧边栏状态管理
const isCollapsed = ref(false)
const sidebarWidth = computed(() => isCollapsed.value ? '64px' : '240px')

// 用户头像
const userAvatar = ref('')

// 当前时间
const currentTime = ref('')
let timeInterval: any

// SignalR状态管理
const signalRStatus = ref<'offline' | 'connecting' | 'online'>('offline')
const signalRStatusText = computed(() => {
  switch (signalRStatus.value) {
    case 'online': return '测试成功连接'
    case 'connecting': return 'SignalR 连接中'
    case 'offline': return 'SignalR 离线'
    default: return 'SignalR 未知状态'
  }
})



// 检查SignalR状态
const checkSignalRStatus = () => {
  if (signalRService.isConnected && signalRService.connection) {
    signalRStatus.value = 'online'
  } else {
    signalRStatus.value = 'offline'
  }
}

// 组件挂载
onMounted(() => {
  getMenu()
  updateTime()
  timeInterval = setInterval(updateTime, 1000)
  
  // 初始检查SignalR状态
  checkSignalRStatus()
  
  // 定期检查SignalR状态（每5秒检查一次）
  setInterval(checkSignalRStatus, 5000)
})

// 组件卸载前清理定时器
onBeforeUnmount(() => {
  if (timeInterval) {
    clearInterval(timeInterval)
  }
})

// 获取菜单数据
const getMenu = () => {
  console.log(useinfo.UserInfo.RidList)
  readBasic.get("api/Menu/GetUserRoleMenu", { 
    params: { ridList: useinfo.UserInfo.RidList.toString() } 
  }).then(res => {
    datas.value = res.data.data
  })
}

// 更新时间
const updateTime = () => {
  const now = new Date()
  currentTime.value = now.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 切换侧边栏
const toggleSidebar = () => {
  isCollapsed.value = !isCollapsed.value
}

// 获取菜单图标
const getMenuIcon = (label: string) => {
  const iconMap: Record<string, any> = {
    '库存管理': Box,
    '用户管理': UserFilled,
    '系统管理': Management,
    '数据分析': DataAnalysis,
    '基础数据': Document,
    '报表管理': TrendCharts,
    '系统设置': SetUp,
    '工具箱': Tools,
    '监控中心': Monitor
  }
  return iconMap[label] || Menu
}

// 获取子菜单图标
const getSubMenuIcon = (label: string) => {
  const iconMap: Record<string, any> = {
    '商品列表': List,
    '添加商品': Plus,
    '库存查询': Search,
    '库存调整': EditPen,
    '用户列表': List,
    '角色管理': UserFilled,
    '权限管理': Management,
    '菜单管理': Menu,
    '数据查看': View,
    '操作记录': Operation,
    '统计报表': TrendCharts,
    '系统日志': Files
  }
  return iconMap[label] || Document
}

// 获取当前面包屑
const getCurrentBreadcrumb = () => {
  const pathMap: Record<string, string> = {
    '/home': '工作台',
    '/inventory': '库存管理',
    '/user': '用户管理',
    '/system': '系统管理',
    '/analytics': '数据分析'
  }
  
  for (const [path, name] of Object.entries(pathMap)) {
    if (route.path.includes(path)) {
      return name
    }
  }
  return '当前页面'
}

// 退出登录
const tui = () => {
  router.push({
    path: '/'
  })
}

// 进入管理页面
const manager = () => {
  router.push({
    path: '/MenuView'
  })
}






</script>

<style scoped>
/* 全局布局 */
.wms-layout {
  height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  overflow: hidden;
}

.wms-container {
  height: 100%;
  background: transparent;
}

/* 顶部导航栏样式 */
.wms-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  border: none;
  height: 70px !important;
  position: relative;
  z-index: 1000;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 24px;
}

/* 品牌区域 */
.brand-section {
  flex: 1;
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.2);
  padding: 8px;
}

.brand-text {
  display: flex;
  flex-direction: column;
}

.system-title {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  color: #ffffff;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.system-subtitle {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
  font-weight: 400;
  letter-spacing: 1px;
}

/* 状态区域 */
.status-section {
  flex: 1;
  display: flex;
  justify-content: center;
}

.status-indicators {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.status-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.1);
  padding: 6px 12px;
  border-radius: 20px;
  backdrop-filter: blur(10px);
}

/* SignalR状态图标样式 */
.status-icon.online {
  color: #67c23a;
  font-size: 16px;
  animation: pulse-glow 2s infinite;
}

.status-icon.connecting {
  color: #e6a23c;
  font-size: 16px;
  animation: spin 1s linear infinite;
}

.status-icon.offline {
  color: #f56c6c;
  font-size: 16px;
}

@keyframes pulse-glow {
  0%, 100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.7;
    transform: scale(1.05);
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.status-text {
  color: #ffffff;
  font-size: 13px;
  font-weight: 500;
}

.system-time {
  color: rgba(255, 255, 255, 0.9);
  font-size: 12px;
  font-family: 'Courier New', monospace;
  font-weight: 500;
}

/* 用户区域 */
.user-section {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 16px;
}

.user-info-card {
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(255, 255, 255, 0.15);
  padding: 8px 16px;
  border-radius: 24px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.user-avatar .avatar {
  border: 2px solid rgba(255, 255, 255, 0.3);
}

.user-details {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-name {
  color: #ffffff;
  font-size: 14px;
  font-weight: 600;
}

.user-role {
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
}

.action-buttons {
  display: flex;
  gap: 12px;
}

.action-btn {
  width: 40px;
  height: 40px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  transition: all 0.3s ease;
}

.action-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.logout-btn:hover {
  background: #f56c6c !important;
  border-color: #f56c6c !important;
}

/* 主容器 */
.main-container {
  height: calc(100vh - 70px);
}

/* 侧边栏样式 */
.wms-sidebar {
  background: #ffffff;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  transition: width 0.3s ease;
  position: relative;
  z-index: 999;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 16px;
  border-bottom: 1px solid #e4e7ed;
  background: #f8f9fa;
}

.menu-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.menu-icon {
  color: #409eff;
  font-size: 20px;
}

.title-text {
  font-size: 16px;
  font-weight: 600;
  color: #303133;
}

.collapse-btn {
  color: #909399;
  transition: all 0.3s ease;
}

.collapse-btn:hover {
  color: #409eff;
}

.menu-scrollbar {
  height: calc(100vh - 170px);
}

/* 菜单样式 */
.wms-menu {
  border: none;
  background: transparent;
}

.main-menu-item {
  margin: 8px 12px;
  border-radius: 8px;
  overflow: hidden;
}

.main-menu-item .el-sub-menu__title {
  height: 48px;
  line-height: 48px;
  background: transparent;
  border-radius: 8px;
  margin-bottom: 4px;
  transition: all 0.3s ease;
  color: #303133;
  font-weight: 500;
}

.main-menu-item .el-sub-menu__title:hover {
  background: linear-gradient(135deg, #409eff, #36d1dc);
  color: #ffffff;
  transform: translateX(4px);
}

.menu-item-icon {
  font-size: 18px;
  margin-right: 8px;
}

.menu-item-text {
  font-size: 14px;
}

.sub-menu-item {
  margin: 4px 0 4px 20px;
  border-radius: 6px;
  overflow: hidden;
}

.sub-menu-item .el-menu-item {
  height: 40px;
  line-height: 40px;
  background: transparent;
  border-radius: 6px;
  transition: all 0.3s ease;
  color: #606266;
  position: relative;
}

.sub-menu-item .el-menu-item:hover {
  background: #ecf5ff;
  color: #409eff;
  transform: translateX(8px);
}

.sub-menu-item .el-menu-item.is-active {
  background: linear-gradient(135deg, #409eff, #36d1dc);
  color: #ffffff;
  position: relative;
}

.sub-menu-item .el-menu-item.is-active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 3px;
  height: 20px;
  background: #ffffff;
  border-radius: 2px;
}

.sub-menu-icon {
  font-size: 16px;
  margin-right: 8px;
}

.sub-menu-text {
  font-size: 13px;
}

/* 侧边栏底部 */
.sidebar-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 16px;
  background: #f8f9fa;
  border-top: 1px solid #e4e7ed;
}

.footer-info {
  text-align: center;
}

.version-info {
  font-size: 12px;
  color: #909399;
  margin-bottom: 4px;
}

.copyright {
  font-size: 11px;
  color: #c0c4cc;
}

/* 主内容区域 */
.wms-main {
  background: #ffffff;
  padding: 0;
  overflow: hidden;
}

.content-wrapper {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.breadcrumb-section {
  background: #ffffff;
  padding: 16px 24px;
  border-bottom: 1px solid #e4e7ed;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
}

.page-breadcrumb {
  font-size: 14px;
}

.main-content {
  flex: 1;
  padding: 24px;
  overflow: auto;
  background: #f5f7fa;
}

/* 展开触发器 */
.expand-trigger {
  position: fixed;
  left: 20px;
  top: 50%;
  transform: translateY(-50%);
  width: 32px;
  height: 32px;
  background: #409eff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(64, 158, 255, 0.4);
  transition: all 0.3s ease;
  z-index: 1001;
}

.expand-trigger:hover {
  background: #66b1ff;
  transform: translateY(-50%) scale(1.1);
}

.expand-trigger .el-icon {
  color: #ffffff;
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .brand-text {
    display: none;
  }
  
  .status-section {
    display: none;
  }
  
  .user-details {
    display: none;
  }
  
  .wms-sidebar {
    position: fixed;
    left: 0;
    top: 70px;
    height: calc(100vh - 70px);
    z-index: 1000;
  }
  
  .main-content {
    padding: 16px;
  }
}

/* 动画效果 */
@keyframes slideInRight {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

@keyframes fadeInUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.wms-header {
  animation: fadeInUp 0.6s ease;
}

.wms-sidebar {
  animation: slideInRight 0.5s ease;
}

/* 滚动条美化 */
:deep(.el-scrollbar__bar) {
  opacity: 0.3;
}

:deep(.el-scrollbar__thumb) {
  background: #409eff;
  border-radius: 4px;
}

:deep(.el-scrollbar__thumb:hover) {
  background: #66b1ff;
}

/* 菜单折叠时的样式调整 */
.wms-menu.el-menu--collapse {
  width: 64px;
}

.wms-menu.el-menu--collapse .main-menu-item {
  margin: 8px 8px;
}

.wms-menu.el-menu--collapse .menu-item-text,
.wms-menu.el-menu--collapse .sub-menu-text {
  opacity: 0;
}

/* SignalR连接状态样式 */
.signalr-status {
  cursor: default;
}

.status-icon.connecting {
  color: #e6a23c;
}

.status-icon.connected {
  color: #67c23a;
}

.status-icon.disconnected {
  color: #f56c6c;
}

.rotating {
  animation: rotate 2s linear infinite;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 实时通知按钮样式 */
.notification-badge {
  position: relative;
}

.notification-btn {
  position: relative;
  overflow: visible;
}

.notification-btn:hover {
  background: #79bbff !important;
  border-color: #79bbff !important;
}

/* 实时通知面板样式 */
.notification-drawer {
  background: #f8f9fa;
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px solid #e4e7ed;
}

.notification-header h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
}

.notification-stats {
  font-size: 12px;
}

.online-indicator {
  color: #67c23a;
  font-weight: 500;
}

.offline-indicator {
  color: #f56c6c;
  font-weight: 500;
}

.notification-content {
  padding: 16px 0;
}

.notification-actions {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid #e4e7ed;
}

.notification-list {
  max-height: calc(100vh - 200px);
  overflow-y: auto;
}

.notification-item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 12px;
  margin-bottom: 8px;
  background: #ffffff;
  border-radius: 8px;
  border-left: 4px solid #e4e7ed;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
}

.notification-item:hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  transform: translateY(-1px);
}

.notification-user_joined {
  border-left-color: #67c23a;
}

.notification-user_left {
  border-left-color: #e6a23c;
}

.notification-inventory_update {
  border-left-color: #409eff;
}

.notification-operation_completed {
  border-left-color: #67c23a;
}

.notification-login_celebration {
  border-left-color: #e6a23c;
}

.notification-user_online {
  border-left-color: #67c23a;
}

.notification-user_offline {
  border-left-color: #f56c6c;
}

.notification-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #f0f2f5;
  color: #606266;
  flex-shrink: 0;
}

.notification-user_joined .notification-icon {
  background: #f0f9ff;
  color: #67c23a;
}

.notification-user_left .notification-icon {
  background: #fdf6ec;
  color: #e6a23c;
}

.notification-inventory_update .notification-icon {
  background: #ecf5ff;
  color: #409eff;
}

.notification-operation_completed .notification-icon {
  background: #f0f9ff;
  color: #67c23a;
}

.notification-login_celebration .notification-icon {
  background: #fdf6ec;
  color: #e6a23c;
}

.notification-user_online .notification-icon {
  background: #f0f9ff;
  color: #67c23a;
}

.notification-user_offline .notification-icon {
  background: #fef0f0;
  color: #f56c6c;
}

.notification-content-text {
  flex: 1;
}

.notification-message {
  font-size: 14px;
  color: #303133;
  line-height: 1.4;
  margin-bottom: 4px;
}

.notification-time {
  font-size: 12px;
  color: #909399;
}

.no-notifications {
  text-align: center;
  padding: 40px 20px;
  color: #909399;
}

.no-notifications-icon {
  font-size: 48px;
  color: #c0c4cc;
  margin-bottom: 16px;
}

.no-notifications p {
  margin: 8px 0;
  font-size: 14px;
}

.no-notifications .hint {
  font-size: 12px;
  color: #c0c4cc;
  line-height: 1.4;
}
</style>
